// pages/index/index.js
const app = getApp()

import * as echarts from "../../../components/echarts/echarts.min"

function lineChart(chart, valueObj) {
  console.log(11111111111);
  console.log(chart);
  console.log(valueObj);
  var option = {
    radar: [{
      indicator: valueObj.xList,
      radius: 60,
      startAngle: 10,
      splitNumber: 5,
      // shape: 'circle',
      name: {
        formatter: '{value}',
        textStyle: {
          color: '#72ACD1',
          padding: [-10, -15] // 控制文字padding
        }
      },
      splitArea: {
        show: true,
        areaStyle: {
          color: [
            'rgba(114, 172, 209, 1)',
            'rgba(114, 172, 209, 1)',
            'rgba(114, 172, 209, 1)',
            'rgba(114, 172, 209, 1)',
            'rgba(114, 172, 209, 1)'
          ],
        }
      },
      axisLine: {
        lineStyle: {
          color: 'rgba(255, 255, 255, 1)'
        }
      },
      splitLine: {
        show: false,
        lineStyle: {
          color: 'rgba(255, 255, 255, 1)'
        }
      }
    }],
    series: [{
      name: '九型人格测试结果',
      type: 'radar',
      symbol: 'rect', // 单个数值点的样式，还可以取值'rect','angle'等
      symbolSize: 6, // 数值点的大小
      data: [{
        value: valueObj.value,
        name: '九型人格测试结果',
        areaStyle: {
          normal: {
            width: 1,
            opacity: 1,
            color: '#F2BFC8',
          },
        },

        label: {
          show: false,
        },
        itemStyle: {
          normal: {
            borderWidth: 0,
            color: '',
            opacity: 0
          },
        },
        splitLine: {
          show: false,
          lineStyle: {
            color: 'rgba(255, 255, 255, 0.5)'
          }
        }

      },]
    }]
  };
  chart.setOption(option);
  return chart;
}


Page({
  /**
   * 页面的初始数据
   */
  data: {
    userName: '',
    testResult: '',
    userTel: '',
    typeList: [],
    flags: -1,
    ec: null
  },

  initChart(valueObj) {

    console.log(valueObj);

    this.selectComponent('#resultEcharts').init((canvas, width, height, dpr) => {
      const chart = echarts.init(canvas, null, {
        width: width,
        height: height,
        devicePixelRatio: dpr
      });
      lineChart(chart, valueObj)
      this.chart = chart;
      canvas.setChart(chart);
      return chart;
    })
  },



  // getuser

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    console.log(echarts);


    console.log(options);
    var obj = JSON.parse(options.obj)
    console.log(obj);
    var typeList = obj.resultList
    this.setData({
      userName: obj.userName,
      testResult: obj.result,
      typeList: typeList,
      userTel: obj.userTel,
      flags: obj.result.flagss
    })

    var xList = [],
      value = []
    typeList.forEach(ele => {
      xList.push({
        name: ele.text,
        max: 15
      })
      value.push(ele.num)
    });


    var obj1 = {
      xList,
      value
    }
    console.log(obj1);
    this.initChart(obj1)
  },
  toWrite() {
    var that = this;
    wx.getSetting({
      success(res) {
        wx.showToast({
          title: res,
        })
        console.log(res);
        if (!res.authSetting['scope.addPhoneContact']) {
          wx.authorize({
            scope: 'scope.addPhoneContact',
            success() {
              console.log("到这里了");
              // 用户已经同意保存到相册功能，后续调用 wx.saveImageToPhotosAlbum 接口不会弹窗询问
              // that.toWriteNow();
              wx.addPhoneContact({
                firstName: that.data.userName,
                mobilePhoneNumber: that.data.userTel,
                remark: that.data.testResult.gtjq,
                success(res) {
                  console.log(res);
                  wx.showModal({
                    title: '提示',
                    content: '测试结束',
                    cancelText: '通讯录',
                    confirmText: '继续测试',
                    complete: (res) => {
                      console.log(res);
                      if (res.cancel) {
                        // 返回通讯录
                        wx.switchTab({
                          url: '/pages/tongxun/tongxun'
                        })
                      }
                      if (res.confirm) {
                        // 继续测试
                        wx.switchTab({
                          url: '/pages/index/index'
                        })
                      }
                    }
                  })
                }
              })

            }
          })
        } else {
          wx.addPhoneContact({
            firstName: that.data.userName,
            mobilePhoneNumber: that.data.userTel,
            remark: that.data.testResult.gtjq,
            success(res) {
              console.log(res);
              wx.showModal({
                title: '提示',
                content: '测试结束',
                cancelText: '通讯录',
                confirmText: '继续测试',
                complete: (res) => {
                  console.log(res);
                  if (res.cancel) {
                    // 返回通讯录
                    wx.switchTab({
                      url: '/pages/tongxun/tongxun'
                    })
                  }
                  if (res.confirm) {
                    // 继续测试
                    wx.switchTab({
                      url: '/pages/index/index'
                    })
                  }
                }
              })
            }
          })
        }
      }
    })
  },
  toBack() {
    wx.redirectTo({
      url: '/packageA/pages/testPage/testPage',
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {


  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() { },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() { },

  /**
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() { },
  onShareTimeline() { },


})